/************************/
/**创建bookDIV**/
var ajaxObject = null;
var nowPageNum = 0;
var iEveryPageNum = 3;
var totalPageNum = 0;


var BookDIv = {
	create:function (ImgSrc,sBookName,fBookAverage,sBookAuthor,sBookUrl){
		var bookDIv = document.createElement("div");
		bookDIv.setAttribute("class","book");
		var img = document.createElement("img");
		img.setAttribute("src",ImgSrc);
		bookDIv.appendChild(img);
		var bookDetail = document.createElement("div");
		bookDetail.setAttribute("class","bookDetail");
		bookDIv.appendChild(bookDetail);
		var bookName = document.createElement("h2");
		bookNameText =document.createTextNode(sBookName);
		bookName.appendChild(bookNameText);
		bookDetail.appendChild(bookName);

		var bookAverage = document.createElement("span");
		var bookAverageP = document.createTextNode("评分: "+fBookAverage);
		bookAverage.appendChild(bookAverageP);
		bookDetail.appendChild(bookAverage);

		var bookAuthor = document.createElement("span");
		var bookAuthorText = document.createTextNode("作者: "+sBookAuthor);
		bookAuthor.appendChild(bookAuthorText);
		bookDetail.appendChild(bookAuthor);

		var userMoveDiv = document.createElement("div");
		userMoveDiv.setAttribute("class","userMoveDiv");
		var userDetailA = document.createElement("a");
		userDetailA.setAttribute("href",sBookUrl);
		userDetailA.setAttribute("target","_blank");
		userDetailA.setAttribute("class","bookUrl");
		var userDetailAText = document.createTextNode("详情");
		userDetailA.appendChild(userDetailAText);
		userMoveDiv.appendChild(userDetailA);
		var userYesA = document.createElement("a");
		userYesA.setAttribute("href","");
		userYesA.setAttribute("class","userYesA");
		var userYesAText = document.createTextNode("添加");
		userYesA.appendChild(userYesAText);
		userMoveDiv.appendChild(userYesA);
		bookDIv.appendChild(userMoveDiv);


		var divClear = document.createElement("div");
		divClear.setAttribute("class","clear");
		bookDIv.appendChild(divClear);


		bookDetail.appendChild(divClear);

		return bookDIv;
	}
};

/***定义xhr**/
function createXHR(){
	if(typeof XMLHttpRequest != "undefined"){
		return new XMLHttpRequest();
	}else if(typeof ActiveXObject != "undefined"){
		if(typeof arguments.callee.activeXString != "string"){
			var versions = ["MSXML2.XMLHttp.6.0,MSXML2.XMLHttp.3.0,MSXML2.XMLHttp"], i ,len;
			for (var i = 0,len=versions.length; i < len; i++) {
				try{
					new ActiveXObject(versions[i]);
					arguments.callee.activeXString = versions[i];
					break;
				}catch(ex){

				}
			};
		}
		return new ActiveXObject(arguments.callee.activeXString);
	}else{
		throw new Error("No XHR Object availale");
	}

}
/***********确定提交******/
addLoadEvent(ajaxClick);

function ajaxClick(){
	var urlAnode = document.getElementById("urlAnode");
	urlAnode.onclick = sureBook;
}


function sureBook(){
	var form = document.forms[1];
	form.submit();
}
/********************/





function cJax(url){
/***使用*/
var xhr = createXHR();
var url = "https://api.douban.com/v2/book/search?q="+url;

xhr.open("get",url,true);

xhr.onreadystatechange = function(){
	if(xhr.readyState == 4){
		if(xhr.status == 200){
			var ajaxText = eval("("+xhr.responseText+")");
			var ajaxDiv = document.getElementById("ajaxDiv");
			var bookLength = ajaxText.books.length;
			var isNull = bookLength > 0 ? false:true ;
			hasMessages(bookLength,iEveryPageNum,ajaxText,ajaxDiv);
			// if(!isNull){
			// 	if(bookLength > 3){
			// 		for (var i = 0; i < 3; i++) {
			// 		var ImgSrc = ajaxText.books[i].images.small,
			//     	sBookName = ajaxText.books[i].title,
			//     	fBookAverage = ajaxText.books[i].rating.average,
			//    	 	sBookAuthor = ajaxText.books[i].author;
			//     	sBookUrl = ajaxText.books[i].alt;
			//     	ajaxDiv.appendChild(BookDIv.create(ImgSrc,sBookName,fBookAverage,sBookAuthor,sBookUrl));
			//         };

			// 	}
			// }else{

			// }

			
			var pop_win_height = document.getElementById("pop").offsetHeight;
			pop_bg.style.height = (pop_win_height + 30) + "px";
			addurl();
		}
	}
};
xhr.send(null);
};
/**搜索分页***/
function hasMessages(totalNum,everyPageNum,ajaxText,ajaxDiv){
	if(totalNum == 0){
		alert("对不起，没有搜索结果！");
	}else{
		if(totalNum <= everyPageNum){
			for (var i = 0; i < everyPageNum; i++) {
				var ImgSrc = ajaxText.books[i].images.small,
			    sBookName = ajaxText.books[i].title,
			    fBookAverage = ajaxText.books[i].rating.average,
			   	sBookAuthor = ajaxText.books[i].author;
			    sBookUrl = ajaxText.books[i].alt;
				ajaxDiv.appendChild(BookDIv.create(ImgSrc,sBookName,fBookAverage,sBookAuthor,sBookUrl));
			};
		}else{
			totalPageNum = Math.floor(totalNum/everyPageNum) + 1; 
			var searchResultsTextP = document.getElementById("searchResultsTextP"); 
			var up = document.getElementById("upPage");
			var down = document.getElementById("downPage");
			searchResultsTextP.innerHTML = "共"+totalPageNum+"页";
			nowPageNum += 1;
			for (var i = 0; i < everyPageNum; i++) {
				var ImgSrc = ajaxText.books[i].images.small,
			    sBookName = ajaxText.books[i].title,
			    fBookAverage = ajaxText.books[i].rating.average,
			   	sBookAuthor = ajaxText.books[i].author;
			    sBookUrl = ajaxText.books[i].alt;
				ajaxDiv.appendChild(BookDIv.create(ImgSrc,sBookName,fBookAverage,sBookAuthor,sBookUrl));
				
			};
			if(nowPageNum != totalPageNum && nowPageNum != 1){
				up.style.display = "block";
				down.style.display = "block";
			}else if(nowPageNum == totalPageNum){
				up.style.display = "block";
				down.style.display = "none";
			}else if(nowPageNum == 1){
				up.style.display = "none";
				down.style.display = "block";
			}
			up.onclick = function(){
				var havedBooks = document.getElementsByClassName("book");
				for (var i = 0; i < havedBooks.length; i++) {
					havedBooks[i].style.display = "none";
				};
				for (var i = (nowPageNum-2)*everyPageNum; i < (nowPageNum - 1)*everyPageNum; i++) {
				var ImgSrc = ajaxText.books[i].images.small,
			    sBookName = ajaxText.books[i].title,
			    fBookAverage = ajaxText.books[i].rating.average,
			   	sBookAuthor = ajaxText.books[i].author;
			    sBookUrl = ajaxText.books[i].alt;
				ajaxDiv.appendChild(BookDIv.create(ImgSrc,sBookName,fBookAverage,sBookAuthor,sBookUrl));
			   };
			   nowPageNum -= 1;
			   if(nowPageNum != totalPageNum && nowPageNum != 1){
				up.style.display = "block";
				down.style.display = "block";
			   }else if(nowPageNum == totalPageNum){
				up.style.display = "block";
				down.style.display = "none";
			   }else if(nowPageNum == 1){
				up.style.display = "none";
				down.style.display = "block";
			   }
			   addurl();
			   var pop_win_height = document.getElementById("pop").offsetHeight;
			   pop_bg.style.height = (pop_win_height + 30) + "px";

			};
			down.onclick = function(){
				var havedBooks = document.getElementsByClassName("book");
				for (var i = 0; i < havedBooks.length; i++) {
					havedBooks[i].style.display = "none";
				};
				for (var i = nowPageNum*everyPageNum; i < (nowPageNum + 1)*everyPageNum; i++) {
				var ImgSrc = ajaxText.books[i].images.small,
			    sBookName = ajaxText.books[i].title,
			    fBookAverage = ajaxText.books[i].rating.average,
			   	sBookAuthor = ajaxText.books[i].author;
			    sBookUrl = ajaxText.books[i].alt;
				ajaxDiv.appendChild(BookDIv.create(ImgSrc,sBookName,fBookAverage,sBookAuthor,sBookUrl));
			   };
			   nowPageNum += 1;
			   if(nowPageNum != totalPageNum && nowPageNum != 1){
				up.style.display = "block";
				down.style.display = "block";
			   }else if(nowPageNum == totalPageNum){
				up.style.display = "block";
				down.style.display = "none";
			   }else if(nowPageNum == 1){
				up.style.display = "none";
				down.style.display = "block";
			   }
			   addurl();
			    var pop_win_height = document.getElementById("pop").offsetHeight;
				pop_bg.style.height = (pop_win_height + 30) + "px";

			};

		}
	}
}





/********事件处理************/
function insertAfter(newElement,targetElement) {
  var parent = targetElement.parentNode;
  if (parent.lastChild == targetElement) {
    parent.appendChild(newElement);
  } else {
    parent.insertBefore(newElement,targetElement.nextSibling);
  }
}


function fsearchSubmit(){
	var searchInput = document.getElementById("searchInput");
	if(searchInput.value == "" || searchInput.value == "URL、书名、作者、IBSN" ){
		alert("请填写内容");
		return false;
	}else{
		var havedBooks = document.getElementsByClassName("book");
				for (var i = 0; i < havedBooks.length; i++) {
					havedBooks[i].style.display = "none";
				};
		var url = searchInput.value;//parseQUrl(searchInput.value);
		if(searchInput.value.indexOf("http://book.douban.com") > -1){

			var subjects = document.getElementsByName("subject");

		}else{
		cJax(checkURL(url));
		
		return false;
		}
	}
}
function submitForm(){
	var searchForm = document.getElementById("searchForm");
	searchForm.onsubmit = fsearchSubmit;
}
addLoadEvent(submitForm);
/****url分析***/
function checkURL(value){
	if(value.indexOf("http://book.douban.com") > -1){
		alert("URL");
		return false;
	}else{
		return value;
	}
}

/********add URL*****************/

function addurl(){
	var userYesA = document.getElementsByClassName("userYesA");
	for (var i = 0; i < userYesA.length; i++) {
		userYesA[i].onclick = function(){
			var subjects = document.getElementsByName("subject");
			var url = this.previousSibling.getAttribute("href");
			for (var j = 0; j < subjects.length; j++) {
				if(subjects[j].value == "http://" || subjects[j].value == ""){
					subjects[j].value = url;
					subjects[j].setAttribute("class","add_book");
					if(j == (subjects.length-1)){
						var more_add_book = document.getElementById("more_add_book");
						var moreParent = more_add_book.parentNode;
						var newInputP = document.createElement("p");
						var newInput  = document.createElement("input");
						newInput.setAttribute("name","subject");
						newInput.setAttribute("type","text");
						newInput.setAttribute("class","add_book");

						newInputP.appendChild(newInput);
						insertAfter(newInputP,subjects[j]);
					}
					break;
				}
			};
		return false;
		}
	};
}

